from re import search

if __name__ == '__main__':
    # 匹配的规则
    pattern = "this"
    # 要匹配的文本内容
    text = "Does this text match the pattern？"

    match = search(pattern=pattern, string=text)

    if match is not None:
        # 匹配的起始位置
        s = match.start()
        # 匹配的终止位置，不包含这个位置
        e = match.end()

        print('Found "{}"\nin "{}"\nfrom {} to {} ("{}")'.format(match.re.pattern, match.string, s, e, text[s:e]))
